# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import execjs
import re
import os

requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

"""
好大夫
注：cookie反爬。第一次返回加密js的cookie生成内容。在浏览器将eval替换成console.log即可查看生成
内容。第一次返回状态码521.将解密后的js生成cookie与第一次的得到cookie进行拼接后，再次请求
链接地址发现2种
1.https://qiuweimin.haodf.com
2.https://www.haodf.com/doctor/DE4rO-XCoLUXyOoakhlkqINLVW.htm
第2种地址，会重新定向，得到url，在url中再次请求。增加 url = res.url
"""


class HaoDaiFu():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        self.cookie = cookiejar.CookieJar()
        ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
            "Accept-Encoding": "gzip, deflate, br",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
            "DNT": "1",
            # "Host": "qiuweimin.haodf.com",
            'Sec-Fetch-Dest': 'document',
            "Sec-Fetch-Mode": "navigate",
            "Sec-Fetch-Site": "none",
            "Upgrade-Insecure-Requests": "1",
        }

    def get_detail(self):
        url = "https://qiuweimin.haodf.com"
        # url = "https://kangweiming.haodf.com/"
        url = "https://www.haodf.com/doctor/DE4rO-XCoLUXyOoakhlkqINLVW.htm"
        # self.headers['Host'] = url.replace("https://", "")


        html = HttpUtils.do_request("GET", url, self.headers, "")
        root = etree.HTML(html.text)
        name = root.xpath('//h1[@class="doctor-name"]/text()')[0]
        ys_zhicheng = root.xpath('//span[@class="positon"]/span/text()')[0]  # 医生职称
        xs_zhicheng = "".join(root.xpath('//span[@class="positon"]/text()')).replace("\n", "").strip()  # 学术职称
        hospital = root.xpath('//p[@class="doctor-faculty"]/a/text()')[0]  # 医院名称
        keshi = root.xpath('//p[@class="doctor-faculty"]/a[2]/text()')[0]  # 科室
        hot = root.xpath('//div[@class="profile-sta"]/p[1]/i/text()')[0]  # 综合推荐热度
        my = root.xpath('//div[@class="profile-sta"]/p[2]/i/text()')[0]  # 在线服务满意度
        wz = root.xpath('//div[@class="profile-sta"]/p[3]/i/text()')[0]  # 在线问诊量

        fwl = root.xpath('//div[@class="block-item person-web"]/div/div[@class="clearfix"][1]/div[2]/text()')[0]  # 总访问
        fwl_z = root.xpath('//div[@class="block-item person-web"]/div/div[@class="clearfix"][2]/div[2]/text()')[0]  # 昨日访问
        wz = root.xpath('//div[@class="block-item person-web"]/div/div[@class="clearfix"][3]/div[2]/text()')[0]  # 总文章
        hz = root.xpath('//div[@class="block-item person-web"]/div/div[@class="clearfix"][4]/div[2]/text()')[0]  # 总患者
        hz_z = root.xpath('//div[@class="block-item person-web"]/div/div[@class="clearfix"][5]/div[2]/text()')[0]  # 昨日诊后报到患者
        hz_weixin = root.xpath('//div[@class="block-item person-web"]/div/div[@class="clearfix"][6]/div[2]/text()')[0]  # 微信诊后报到患者
        hz_qz = root.xpath('//div[@class="block-item person-web"]/div/div[@class="clearfix"][7]/div[2]/text()')[0]  # 总诊后报到患者
        hz_tp = root.xpath('//div[@class="block-item person-web"]/div/div[@class="clearfix"][8]/div[2]/text()')[0]  # 患者投票
        gxx = root.xpath('//div[@class="block-item person-web"]/div/div[@class="clearfix"][9]/div[2]/text()')[0]  # 感谢信
        liwu = root.xpath('//div[@class="block-item person-web"]/div/div[@class="clearfix"][10]/div[2]/text()')[0]  # 心意礼物
        sc_onlie = root.xpath('//div[@class="block-item person-web"]/div/div[@class="clearfix"][11]/div[2]/text()')[0]  # 上次在线
        create = root.xpath('//div[@class="block-item person-web"]/div/div[@class="clearfix"][12]/div[2]/text()')[0]  # 开通时间

        # # 检索是否有【电话咨询】标签
        # for s in root.xpath('//ul[@class="clearfix f16"]/li'):
        #     if s.xpath('./a/text()')[0] == "电话咨询":
        #         url_tel = "https:" + s.xpath('./a/@href')[0]
        #         break
        # html = HttpUtils.do_request("GET", url_tel, self.headers, "")
        # root1 = etree.HTML(html.text)

    # 封装请求方法
    def get_request(self, method, url, headers, data):
        # cookies = self.get_cookie_dict("JSESSIONID=D79B2E69AADDABD4A2A0B5352C4A1987")
        try:
            if method == "POST":
                if headers == "":
                    if url.find("https") == 0:
                        res = requests.post(url, data=data, verify=False)
                    else:
                        res = requests.post(url, data=data)
                else:
                    if url.find("https") == 0:
                        res = requests.post(url, headers=headers, data=data, verify=False)
                    else:
                        res = requests.post(url, headers=headers, data=data)
            elif method == "GET":
                if headers == "":
                    if url.find("https") == 0:
                        res = requests.get(url, params=data, verify=False)
                    else:
                        res = requests.get(url, params=data)
                else:
                    if url.find("https") == 0:
                        res = requests.get(url, headers=headers, params=data, verify=False)
                    else:
                        res = requests.get(url, headers=headers, params=data)
            else:
                res = None
        except Exception as ex:
            print("-------------访问错误------------")
            print(ex)
            return HttpUtils.do_request(method, url, headers, data)
        else:
            # if res.status_code != 200:
            #     print(f"-------------返回状态码:{res.status_code}------------")
            #     return HttpUtils.do_request(method, url, headers, data)
            if res.status_code == 521:
                url = res.url
                print(f"-------------返回状态码:{res.status_code}------------")
                # func_return=res.text.replace('eval','return')
                # content=execjs.compile(func_return)
                # evaled_func=content.call('f')
                aa = res.cookies
                aa = '; '.join(['='.join(item) for item in aa.items()])
                # 解密Cookie
                cookie = self.parse_js(res.text)
                bb = cookie.split(';')[0]
                headers['Cookie'] = aa + ";" + bb
                return HttpUtils.do_request(method, url, headers, data)
            if res.status_code == 403 or res.status_code == 404:
                aa = res.cookies
                aa = '; '.join(['='.join(item) for item in aa.items()])
                id = re.findall(r"host_user_id=(.*?)&", res.url)[0]
                headers['Cookie'] = "g=HDF.158.5def325029d24; UM_distinctid=16eee5c93c0af-030f30b44224bb-2393f61-144000-16eee5c93c1ae; _ga=GA1.2.1270518259.1575957076; _gid=GA1.2.11996686.1575957076; Hm_lvt_dfa5478034171cc641b1639b2a5b717d=1575957076; PHPSESSID=038abt0dp5800kn6ikd2nf1md2; newaskindex=1; __jsluid_s=024bcc9e0f6dc8d3e6e1b9aad8f1183f; CNZZDATA-FE=CNZZDATA-FE; Hm_lpvt_dfa5478034171cc641b1639b2a5b717d=1575984702"
                url = f"https://zixun.haodf.com/ask.php?from=space&host_user_id={id}&case_title="
                return HttpUtils.do_request(method, url, headers, data)
        return res

    def parse_js(self, text):
        jj = r'const jsdom = require("jsdom");const { JSDOM } = jsdom;const dom = new JSDOM("<!DOCTYPE html><p>Hello world</p>");window = dom.window;document = window.document;XMLHttpRequest = window.XMLHttpRequest;'
        js_code = re.findall("<script>(.*?)</script>", text)
        js = js_code[0]

        # func_return = js.replace('eval', 'return')
        # resHtml = "function getClearance(){" + func_return + "};"
        # ctx = execjs.compile(resHtml)
        # temp1 = ctx.call('getClearance')
        # print(temp1)
        # s = 'var a' + temp1.split('document.cookie')[1].split("Path=/;'")[0] + "Path=/;';return a;"
        # s = re.sub(r'document.create.*?firstChild.href', '"{}"'.format('http://www.66ip.cn/1.html'), s)
        # print('s--->',s)
        # resHtml = "function getnewClearance(){" + s + "};"
        # ctx = execjs.compile(resHtml)
        # jsl_clearance = ctx.call('getnewClearance')
        # __jsl_clearance = str(jsl_clearance).split(';')[0]
        # print(jsl_clearance)

        js = js.replace("eval(", "var zzz = (")
        ctx = execjs.compile(jj + js, cwd=r'D:\Program Files\nodejs\node_global\node_modules')
        zzz = ctx.eval("zzz")
        print(zzz)
        c_start = zzz.find("cookie")
        c_end = zzz.find("Path=/;'") + len("Path=/;'")
        cookie_code = zzz[c_start:c_end]
        cookie_js = "var " + zzz[c_start:c_end]

        ctx = execjs.compile(jj + cookie_js, cwd=r'D:\Program Files\nodejs\node_global\node_modules')
        print(cookie_js)
        cookie_str = ctx.eval("cookie")
        print(cookie_str)
        return cookie_str

    # 将cookies字符串转换dict格式
    def get_cookie_dict(self, str):
        itemDict = {}
        items = str.split(';')
        for item in items:
            arr = item.split('=')
            key = arr[0].replace(' ', '')
            value = arr[1]
            itemDict[key] = value
        return itemDict

if __name__ == '__main__':
    haodaifu = HaoDaiFu()
    haodaifu.get_detail()